<?php
/**
 * Pert PHP Library
 * SQLGenerator
 * LingualNamedTable Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert\PostgreSQLGenerator;

require_once __DIR__ . '/Table.php';

class LingualNamedTable extends Table
{
	private $privilegedLanguageArray;

	private $alternativeName;

	final public function __Construct($name,
			$schema,
			Array $columnArray,
			Array $privilegedLanguageArray,
			$alternativeName)
	{
		parent::__Construct($name,
				$schema,
				$columnArray);

		$this->privilegedLanguageArray = $privilegedLanguageArray;

		$this->alternativeName = $alternativeName;
	}

	final protected function selectClauseNameCoreSQL($prefix = '')
	{
		return '"' . $prefix . $this . '_name"."Name"';
	}

	final protected function fromClauseCoreSQL($prefix = '')
	{
		require_once __DIR__ . '/ArrayInput.php';

		$privilegedLanguageArrayInput = new ArrayInput($this->privilegedLanguageArray);

		$sQL = parent::fromClauseCoreSQL($prefix) . '
JOIN "' . $this->Schema() . '"."' . $this . '_name" "' . $prefix . $this . '_name" ON "' . $prefix . $this . '_name"."' . $this . '" = "' . $prefix . $this . '".id
		AND "' . $prefix . $this . '_name"."Language" IN ' . $privilegedLanguageArrayInput->SQL();

		if($this->alternativeName)
			$sQL .= '
		AND "' . $prefix . $this . '_name"."Alternative" = false';

		return $sQL;
	}
}
